package com.example.mobifin_busticket.views.dialogs;

import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.database.sqlite.SQLiteDatabase;
import android.os.Handler;
import android.view.View;
import android.view.Window;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.example.mobifin_busticket.R;
import com.example.mobifin_busticket.controller.networkmanager.Async_getActivationKey;

public class RegistrationDialog extends Dialog implements
		android.view.View.OnClickListener {
	SQLiteDatabase db;
	TextView tv1, tv2, tv3;
	EditText kyc, phno, email;
	Button b1, b2, ok, cncle;
	Context context;
	Intent i;
	Handler handler;

	// emulator start kar

	public RegistrationDialog(Context mcontext) {
		super(mcontext);
		context = mcontext;
		handler = new Handler();
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.registration);
		kyc = (EditText) findViewById(R.id.txtinfo);
		phno = (EditText) findViewById(R.id.txtphno);
		email = (EditText) findViewById(R.id.txtmail);
		ok = (Button) findViewById(R.id.btn_ok);
		cncle = (Button) findViewById(R.id.btn_Cancel);
		ok.setOnClickListener(this);
		System.out.println("registering account");
	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		if (v == ok) {

			System.out.println("Registration process");
			final String strKyc = kyc.getText().toString();
			final String strPh = phno.getText().toString();
			final String strEmail = email.getText().toString();
			if (validation_chk() == true) {

				// AppPreferences appPreferences=new AppPreferences(mContext);
				// appPreferences.setEmailAddress(etEmail.getText().toString().trim());

				new Async_getActivationKey(context, strKyc, strPh, strEmail,
						RegistrationDialog.this).execute();

			}
		} else {
			dismiss();
		}
	}

	private boolean validation_chk() {
		String MobilePattern = "[0-9]{11}";
		String emailPattern = "^[_A-Za-z0-9-]+(\\.[_A-Za-z0-9-]+)*@[A-Za-z0-9]+(\\.[A-Za-z0-9]+)*(\\.[A-Za-z]{2,})$";
		String InputEmail = email.getText().toString().trim();
		boolean flag = true;
		String kycno = "1234";

		if (kyc.getText().toString().trim().length() == 0) {
			flag = false;
			kyc.setError(getContext().getResources().getString(
					R.string.valid_kyc));

		}
		if (!phno.getText().toString().matches(MobilePattern)) {
			flag = false;
			phno.setError(getContext().getResources().getString(
					R.string.valid_ph));

		}
		/*
		 * if (!InputEmail.matches(emailPattern)) { flag = false;
		 * email.setError(getContext().getResources().getString(
		 * R.string.valid_email)); }
		 */

		return flag;
	}

}
